$(function () {
    let examId = getQueryString("examId");
    let url = 'lanling/examdatails/list';
    if (examId) {
        url += '?examId=' + examId;
    }
    $("#jqGrid").jqGrid({
        url: baseURL + url,
        datatype: "json",
        colModel: [
            {label: '试卷名', name: 'examName', index: 'examName', width: 120, align: "center"},
            {label: '学员姓名', name: 'userName', index: 'userName', width: 80, align: "center"},
            {label: '问题详情', name: 'questionContent', index: 'questionContent', width: 180, align: "center"},
            {
                label: '学生答案', name: 'studentAnswer', index: 'student_answer', width: 80, align: "center",
                formatter: function (value) {
                    return (value === null || value === '') ? '空' : value;
                }
            },
            {
                label: '做题状态', name: 'state', index: 'state', width: 80, align: "center",
                formatter: function (value) {
                    return value === 1 ?
                        '<span class="label label-danger">未做题</span>' :
                        '<span class="label label-success">已答题</span>';
                }
            },
            {
                label: '答题状态', name: 'isTrue', index: 'is_true', width: 80, align: "center",
                formatter: function (value) {
                    return value === 1 ?
                        '<span class="label label-success">正确</span>' :
                        '<span class="label label-danger">错误</span>';
                }
            },
            {label: '得分', name: 'score', index: 'score', width: 80, align: "center"},
            // {label: '创建时间', name: 'createDate', index: 'create_date', width: 80, align: "center"},
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        examDatails: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.examDatails = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.examDatails.id == null ? "lanling/examdatails/save" : "lanling/examdatails/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.examDatails),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "lanling/examdatails/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "lanling/examdatails/info/" + id, function (r) {
                vm.examDatails = r.examDatails;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});